package com.newa.actions;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.imgscalr.Scalr;

import com.newa.beans.Section;
import com.newa.beans.Work;
import com.newa.dao.DAOSection;
import com.newa.dao.DAOWorks;
import com.newa.listeners.Listener;
import com.newa.utils.HibUtil;
import com.opensymphony.xwork2.ActionSupport;

public class addWork extends ActionSupport {
	private Logger LOG = Logger.getLogger(addWork.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int section_id;
	private File[] photos;
	private String[] photosFileNames;
	private String[] photosContentTypes;
	private File logo;
	private String logoFileName;
	private String logoContentType;
	private Work work;
	
	public BufferedImage getResizedInage(File file) throws IOException{
		BufferedImage img = ImageIO.read(file);

		float w = img.getWidth(); w = w/(float)(3.625);
		float h = img.getHeight(); h = h/(float)(3.64516129);
		BufferedImage res = Scalr.resize(img, (int)w, (int)h, null);
		return res;
	}
	
	public String getType(String mime){
		StringTokenizer tok = new StringTokenizer(mime,"/");
		tok.nextToken();
		String res = tok.nextToken();
		return res;
	}
	
	@Override
	public String execute() throws Exception {
		
		Session session = HibUtil.getSessionFactory().openSession();
		DAOSection daos = new DAOSection(session);
		Section sec = daos.getSectionById(section_id);
		session = daos.closeSession();
		DAOWorks daow = new DAOWorks(session);
		try {
			System.out.println(ServletActionContext.getServletContext()
					.getRealPath("/photos"));
			String path = ServletActionContext.getServletContext().getRealPath(
					"/photos");
			//String path = "/newastaldo/photos";
			if (logo != null) {
				File logoToCreate = new File(path, logoFileName);
				FileUtils.copyFile(logo, logoToCreate);
				work.setLOGO_LINK(path + "/" + logoFileName);
			}
			work.setSECTION_ID(sec);
			String plinks = "";
			String lplinks = "";
			BufferedImage res;
			if (photos != null) {
				for (int i = 0; i < photos.length; i++) {
					File ph = photos[i];
					File phToCreate = new File(path, photosFileNames[i]);
					FileUtils.copyFile(photos[i], phToCreate);
					res = getResizedInage(new File(path+"/"+photosFileNames[i]));
					File outputfile = new File(path+"/"+"l"+photosFileNames[i]);
				    ImageIO.write(res, getType(photosContentTypes[i]), outputfile);
					plinks += path + "/" + photosFileNames[i] + ";";
					lplinks += path+"/"+"l"+photosFileNames[i]+";";
				}
				plinks = plinks.substring(0, plinks.length() - 1);
				lplinks = lplinks.substring(0, lplinks.length() - 1);
				work.setPHOTO_LINKS(plinks);
				work.setLPHOTO_LINKS(lplinks);
			}
			daow.createWork(work);
		} catch (Exception e) {
			LOG.warn(e);
			throw new RuntimeException(e);
		} finally {
			daow.closeSession().close();
		}
		return "success";
	}

	public Work getWork() {
		return work;
	}

	public void setWork(Work work) {
		this.work = work;
	}

	public void setPhotos(File[] photos) {
		this.photos = photos;
	}

	public void setPhotosFileName(String[] photosFileNames) {
		this.photosFileNames = photosFileNames;
	}

	public void setPhotosContentType(String[] photosContentTypes) {
		this.photosContentTypes = photosContentTypes;
	}

	public void setLogo(File logo) {
		this.logo = logo;
	}

	public void setLogoFileName(String logoFileName) {
		this.logoFileName = logoFileName;
	}

	public void setLogoContentType(String logoContentType) {
		this.logoContentType = logoContentType;
	}

	public String[] getPhotosContentType() {
		return photosContentTypes;
	}

	public File[] getPhotos() {
		return photos;
	}

	public String[] getPhotosFileName() {
		return photosFileNames;
	}

	public File getLogo() {
		return logo;
	}

	public String getLogoFileName() {
		return logoFileName;
	}

	public String getLogoContentType() {
		return logoContentType;
	}

	public String prepare() {
		return "prepare";
	}

	public int getSection_id() {
		return section_id;
	}

	public void setSection_id(int section_id) {
		this.section_id = section_id;
	}

}
